package top.porchwood.jweb.mcsm;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.system.ApplicationHome;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import top.porchwood.jweb.mcsm.config.MCSMConfig;
import top.porchwood.jweb.mcsm.util.ApplicationInit;

@SpringBootApplication
public class MCSMApplication implements WebMvcConfigurer, ApplicationRunner {
    /**
     * 作为Jar模式直接启动
     * @param args 程序参数
     */
    public static void main(String[] args) {
        ApplicationInit.CheckFristLaunch();
        SpringApplication.run(MCSMApplication.class, args);
    }

    /**
     * Spring boot 首次启动时执行
     * @param args 启动参数
     * @throws Exception 抛出错误
     */
    @Override
    public void run(ApplicationArguments args) throws Exception {
        s_config = config;
    }

    @Autowired
    private MCSMConfig config;
    private static MCSMConfig s_config;

    public static MCSMConfig getConfig() {
        return s_config;
    }


    private static final String runningPath = new ApplicationHome().getDir().getAbsolutePath();

    public static String getRunningPath() {
        return runningPath;
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/**")
                //.addResourceLocations("classpath:META-INF/resources/")
                //.addResourceLocations("classpath:/resources/")
                //.addResourceLocations("classpath:/public/")
                .addResourceLocations("classpath:/static/")
                .addResourceLocations("file:" + config.getFilePath());
    }
}
